/**
 * @author helio
 */
if (!SALE.window) SALE.window = {}

if (!SALE.window.ProductsSelectWindow) {
	SALE.window.ProductsSelectWindow = Ext.extend(SALE.basicGridPanel, {
		
		layout: 'fit',
		initComponent: function(){
			this.detailsJson = {};
			this.detailsStore = new Ext.data.Store({
			    proxy: new Ext.ux.data.PagingMemoryProxy(this.detailsJson),
			    //remoteSort:true,
			    reader: new Ext.data.JsonReader({
					root: 'products',
					remoteSort: true,
			        fields: ['pcode', 'pname', 'punit',{name:'pprice',type:'float'},{name:'buy_price',type:'float'}, 'specs', 'model', {name:'snumber',type:'float'}]
			    })
			});
			
			// create paging bar
			var pagingBar = new Ext.PagingToolbar({
		        pageSize: 25,
		        store: this.detailsStore,
		        displayInfo: true,
				
				plugins: new Ext.ux.ProgressBarPager()
		    });
					
			// create the grid
			this.detailsGrid = new Ext.grid.GridPanel({
				region:'center',
				store: this.detailsStore,
				viewConfig: {
			        forceFit: true,
					enableRowBody:true,
		            showPreview:true
			    },
				
				loadMask: true,
				stripeRows: true,
				border: true,
				autoExpandColumn: 'name',
								
				columns: [new Ext.grid.RowNumberer(), {
	           		header: "商品编号",
	           		dataIndex: 'pcode',
	           		width: 60
	       		},{
	           		header: "商品名称",
	           		dataIndex: 'pname',
	           		align: 'left'
	        	}, {
					header: "在库数量",
					dataIndex: 'snumber',			
					width: 60,
					align: 'right',
					renderer: SALE.Utils.stockRenderer.createDelegate(this)
				}, {
	           		header: "规格",
	           		dataIndex: 'specs',
	           		width: 50
	        	}, {
	           		header: "型号",
	           		dataIndex: 'model',
	           		width: 50
	        	}],
				
				sm: new Ext.grid.CheckboxSelectionModel(),
				
				tbar:[new Ext.form.ComboBox({
					id: 'querytype',
					tpl: comboxTpl,
				    triggerAction: 'all',
				    lazyRender:true,
				    forceSelection:true,
                 	selectOnFocus: true,
				    mode: 'local',
				    width: 100,
				    store: new Ext.data.ArrayStore({
				        id: 0,
				        fields: [
				            'code',
				            'name'
				        ],
				        data: [[1, '按编号查询'], [2, '按名称查询']]
				    }),
				    valueField: 'code',
				    displayField: 'name',
				    value:1
				}), '-', {
					id: 'queryvalue',
                	xtype:'trigger',
                    triggerClass: 'x-form-search-trigger',
                    width: 200,
                    onTriggerClick: this.queryStocksProducts.createDelegate(this),
                    listeners: {
        				'show': this.queryStocksProducts.createDelegate(this)
        			}
                }, '->', {
		            //text:'添加商品',
		            tooltip:'添加商品信息',
		            iconCls:'add',
					handler: this.showProductEditWindow.createDelegate(this)
		        }],
                
                bbar: pagingBar,
                
                buttons: [{
					text: '选中',
					listeners: {
						"click": this.selectHandler.createDelegate(this)
					}
				},{
					text: '取消',
					listeners:{
						"click":this.cancelHandler.createDelegate(this)
					}
				}]
			});
			
			this.items = this.detailsGrid;
			
			SALE.window.ProductsSelectWindow.superclass.initComponent.call(this);
		},
		
		queryStocksProducts : function() {
			var querytype = Ext.getCmp('querytype').getValue();
			var queryvalue = Ext.getCmp('queryvalue').getValue();
			var condition = {};
			if(querytype == 1) {
				condition.code = queryvalue;
			} else if(querytype == 2) {
				condition.name = queryvalue;
			} 
			
			if(queryvalue){
				SALE.Action.getProductsSaleListJson(this.initlizeDataHandler.createDelegate(this), condition);
			}
		},
		
		initlizeDataHandler : function(resultJson) {
			this.detailsJson['products'] = resultJson.products;
			this.detailsStore.load({params:{start:0, limit:25}});
		},
		
		selectHandler : function() {
			var recordList = this.detailsGrid.getSelectionModel().getSelections();
			if(recordList.length == 0) {
				SALE.Utils.showMessageBox('请先选择一条数据！');
			} else if(recordList.length > 1) {
				SALE.Utils.showMessageBox('每次只能选择一条数据，请确认只选中了一条数据！');
			} else {
				var record = recordList[0];
				if(SALE.window.showProductsSelectWindow.selectCallback) {					
					SALE.window.showProductsSelectWindow.selectCallback(record);
				}
				this.hide();
			}
		},
		
		cancelHandler : function() {
			this.hide();
		},
		
		showProductEditWindow : function() {
			SALE.window.showProductsEditWindow(null, null, 'new');
		}
	});
	
	Ext.reg('productsselectwindow', SALE.window.ProductsSelectWindow);
}

SALE.window.showProductsSelectWindow = function(selectCallback, condition) {
	SALE.window.showProductsSelectWindow.selectCallback = selectCallback;
	SALE.window.showProductsSelectWindow.condition = condition;
	if(!this.selectWin) {
		this.selectWin = new Ext.Window({
		 		
		 	title: '库存商品查询',
			closable: true,
			//collapsible  : true,
			//maximizable: true,
			width: 600,
			height: 400,
			layout: 'fit',
			closeAction :'hide',
					
			items: {
				border: false,
				frame: true,
				xtype : "productsselectwindow"
			},
			
			listeners: {
				'show': function(){
		    		if(SALE.window.showProductsSelectWindow.condition) {
		    			var condition = SALE.window.showProductsSelectWindow.condition;
		    			Ext.getCmp('querytype').setValue(condition.querytype);
						Ext.getCmp('queryvalue').setValue(condition.queryvalue);
						Ext.getCmp('queryvalue').show();
		    		}			    		
				}
			}
			
		});
	}
	this.selectWin.show();
}